package com.ruoyi.web.controller.erp.accounting;


import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.erp.accounting.domain.Accounting;
import com.ruoyi.erp.accounting.service.AccountingService;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/accounting")
public class AccountingController extends BaseController {
  @Autowired
  private AccountingService accountingService;

  @PreAuthorize("@ss.hasPermi('accounting:accounting:list')")
  @PostMapping("/list")
  public TableDataInfo getList(@RequestBody Accounting accounting){
    List<Accounting> accountingList=accountingService.getList(accounting);
    return getDataTable(accountingList);
  }

  @PreAuthorize("@ss.hasPermi('accounting:accounting:add')")
  @PostMapping("/insert")
  public AjaxResult addAccounting(@RequestBody Accounting accounting){
    accountingService.addAccounting(accounting);
    return AjaxResult.success("添加成功");
  }

  @PreAuthorize("@ss.hasPermi('accounting:accounting:edit')")
  @PostMapping("/update")
  public AjaxResult updateAccounting(@RequestBody Accounting accounting){
    try {
      accountingService.updateAccounting(accounting);
    } catch (Exception e) {
      e.printStackTrace();
      return AjaxResult.success("修改失败");
    }
    return AjaxResult.success("修改成功");
  }

  @PreAuthorize("@ss.hasPermi('accounting:accounting:remove')")
  @PostMapping("/delete/{ids}")
  public AjaxResult deleteAccounting(@PathVariable @RequestBody String [] ids){
    return toAjax(accountingService.deleteAccounting(ids));
  }

  @PreAuthorize("@ss.hasPermi('accounting:accounting:list')")
  @GetMapping("/edit/{rowkey}")
  public AjaxResult findAccountingByRowkey(@PathVariable String rowkey){
    return AjaxResult.success(accountingService.findAccountingByRowkey(rowkey));
  }

  @PreAuthorize("@ss.hasPermi('accounting:accounting:export')")
  @PostMapping("export")
  public AjaxResult export(@Validated @RequestBody Accounting accounting){
    List<Accounting> accountingList = accountingService.getList(accounting);
    ExcelUtil<Accounting> util = new ExcelUtil<Accounting>(Accounting.class);
    return util.exportExcel(accountingList, "会计组");
  }

}
